<%@page import="org.apache.ibatis.session.SqlSession" %>
<%@page import="java.io.InputStream" %>
<%@page import="org.apache.ibatis.io.Resources" %>
<%@page import="org.apache.ibatis.session.SqlSessionFactoryBuilder" %>
<%@page import="org.apache.ibatis.session.SqlSessionFactory" %>
<%@page import="com.blackhorse.dao.UserMapper" %>
<%@page import="com.blackhorse.domain.User" %>
<%@page import="java.util.List" %>
<%@page import="java.io.IOException" %>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<body>
<h2>MyBatis使用JNDI连接池</h2>
<%
    SqlSession sqlSession = null;
    try {
        //1.读配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(is);
        //3.使用工厂生产SqlSession
        sqlSession = sqlSessionFactory.openSession();
        //4.获取Dao的代理对象mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //5.执行操作
        List<User> users = mapper.findAll();
        for (User user : users) {
            out.println(user + "<br>");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //6.释放资源
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
%>
</body>
</html>
